package vmcs.utility;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 * 
 * 
 * @author arun
 * 
 */

public class ReflectionUtils {

	/**
	 * Static class to convert fields in a property file into a String which is used in the
	 * operations involving the files
	 * 
	 * @param object
	 *            Object that will be converted to string
	 * @return string that StringBuilder retrieves
	 * @exception if
	 *                Stringbuilder is unable to append fields
	 */
	public static String toString(Object object) {

		StringBuilder stringBuilder = new StringBuilder();

		ArrayList<Field> fieldsList=new ArrayList<Field>();
    	fieldsList.addAll(Arrays.asList(object.getClass().getDeclaredFields()));
    	fieldsList.addAll(getInheritedFields(object.getClass().getSuperclass()));

		for (Field field : fieldsList) {
			field.setAccessible(true);
			try {
				stringBuilder.append(field.getName() + "=" + field.get(object)
						+ "\n");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return stringBuilder.toString();
	}
	
	
	 public static List<Field> getInheritedFields(Class<?> type) {
	        List<Field> fields = new ArrayList<Field>();
	        for (Class<?> c = type; c != null; c = c.getSuperclass()) {
	            fields.addAll(Arrays.asList(c.getDeclaredFields()));
	        }
	        return fields;
	    }
	
	
}
